package pacmanreturns;

import java.awt.*;

class Pacman extends Sprite {
	public static final int WIDTH = 18;
	public static final int HEIGHT = 18;
	public static final Color COLOR = Color.YELLOW;

	private int chomp = 45;
	private boolean chompClose;

	public Pacman(Game inCanvas, Maze inMaze) {
		super(inCanvas, inMaze);
	}

	private void calculateChomp() {
		if (chompClose) {
			chomp -= 2;
		} else {
			chomp += 2;
		}
		if (chomp > 45) {
			chompClose = true;
		} else if (chomp < 2) {
			chompClose = false;
		}
	}

	private void checkBounds() {
		if (x < (0 - WIDTH)) {
			x = Game.WIDTH;
		} else if (x > Game.WIDTH) {
			x = (0 - WIDTH);
		} else if (y < (0 - HEIGHT)) {
			y = Game.HEIGHT;
		} else if (y > Game.HEIGHT) {
			y = (0 - HEIGHT);
		}
	}

	public void move() {
		if (maze.canGo(nextDirection, x, y)) {
			direction = nextDirection;
			nextDirection = Game.STOP;
		}
		if (!maze.isSpace(direction, x, y)) {
			return;
		}
		switch (direction) {
			case Game.RIGHT: x += maze.SPEED; break;
			case Game.UP:    y -= maze.SPEED; break;
			case Game.LEFT:  x -= maze.SPEED; break;
			case Game.DOWN:  y += maze.SPEED; break;
			default: break;
		}
		checkBounds();
		maze.removeDot(direction, x, y);
	}

	public void draw() {
		int angle = 0;
		switch (direction) {
			case Game.RIGHT: angle = 0; break;
			case Game.UP:    angle = 90; break;
			case Game.LEFT:  angle = 180; break;
			case Game.DOWN:  angle = 270; break;
			default: break;
		}

		move();
		calculateChomp();
		int startAngle = angle + chomp;
		int arcAngle = 360 - (2 * chomp);

		canvas.setColor(COLOR);
		canvas.arc(x + 1, y + 1, WIDTH, HEIGHT, startAngle, arcAngle);
	}
}